﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace TripLOA.GUI.Controllo_Ascx
{
    public partial class ExampleTemplate : System.Web.UI.UserControl
    {
        private Storage.EntityModels.VehicleTypes _val;

        public Storage.EntityModels.VehicleTypes Val
        {
            get { return _val; }
            set { _val = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.updateControl();
        }
        public bool save(Groupware.Trip gt,bool isdraft)
        {
           
            switch (_val)
            {
                case Storage.EntityModels.VehicleTypes.Airplane:
                case Storage.EntityModels.VehicleTypes.Ship:
                case Storage.EntityModels.VehicleTypes.Train:
                    String commenti="";
                    if (!TextBoxComments.Text.Equals(String.Empty)) commenti = commenti + TextBoxComments.Text;
                    if (!txtCompany.Text.Equals(String.Empty)) commenti = commenti + txtCompany.Text;
                    if (!txtSetSeat.Text.Equals(String.Empty)) commenti = commenti + txtSetSeat.Text;
                    if (!commenti.Equals(String.Empty)) gt.Notes = commenti;    
                    break;
                
                case Storage.EntityModels.VehicleTypes.Bike:
                    if (!TextBoxComments.Text.Equals(String.Empty)) gt.Notes=TextBoxComments.Text;
                    break;
                case Storage.EntityModels.VehicleTypes.Car:
                case Storage.EntityModels.VehicleTypes.Motorcycle:
                    if (!TextBoxComments.Text.Equals(String.Empty)) gt.Notes = TextBoxComments.Text;
                    if (isdraft)
                    {
                        if (!txtSeats.Text.Equals(String.Empty))
                        {
                            rvseat.Validate();
                            if (!rvseat.IsValid)
                            {
                                aggiorna.Update();
                                return false;
                            }
                            try
                            {
                               gt.FreeSeats = Byte.Parse(txtSeats.Text);
                            }
                            catch (Exception)
                            {
                                aggiorna.Update();
                                return false;
                            }
                            
                        }
                    }
                    else
                    {
                        if (txtSeats.Text.Equals(string.Empty))
                        {
                            rvseat.IsValid = false;
                            aggiorna.Update();
                            return false;
                        }
                        rvseat.Validate();
                        if (!rvseat.IsValid)
                        {
                            aggiorna.Update();
                            return false;
                        }
                        try
                        {
                            gt.FreeSeats = Byte.Parse(txtSeats.Text);
                        }
                        catch (Exception)
                        {
                            aggiorna.Update();
                            return false;
                        }
                        
                    }
                    decimal budget = 0;
                    try
                    {   
                        budget = decimal.Parse(txtBudget.Text);
                    }
                    catch (Exception)
                    {
                        budget = 0;
                    }

                    if(budget!=0)gt.EstimatedBudget = budget;
                    float deviation = 0;
                    try
                    {
                        deviation = float.Parse(txtDeviation.Text);
                    }
                    catch (Exception)
                    {
                        deviation = 0;
                    }

                    if(deviation!=0)gt.DeviationRadius = deviation;
                    if (_val == Storage.EntityModels.VehicleTypes.Car)
                    {
                        gt.AcceptSmoker = CheckBoxAcceptSmoker.Checked;
                    }
                    gt.RequireLicenced = CheckBoxRequestDriverLicense.Checked;
                    break;
                /*case Storage.EntityModels.VehicleTypes.Motorcycle:
                    if (!TextBoxComments.Text.Equals(String.Empty)) gt.Notes = TextBoxComments.Text;
                    gt.RequireLicenced = CheckBoxRequestDriverLicense.Checked;
                   
                    break;*/
                
               
                
            }
            return true;
        }

        public TextBox getTxtComment()
        {
            return TextBoxComments;
        }

        public TextBox getTxtCompany()
        {
            return txtCompany;
        }

        public TextBox getTxtSetSeat()
        {
            return txtSetSeat;
        }

        public TextBox getTxtSeats()
        {
            return txtSeats;
        }

        public TextBox getTxtBudget()
        {
            return txtBudget;
        }

        public TextBox getTxtDeviation()
        {
            return txtDeviation;
        }

        public CheckBox getCheckBoxDriverLicense()
        {
            return CheckBoxRequestDriverLicense;
        }

        public CheckBox getCheckBoxSmoker()
        {
            return CheckBoxAcceptSmoker;
        }

        public void updateControl()
        {
            rowNotes.Visible = true;
            switch (_val)
            {
                case Storage.EntityModels.VehicleTypes.Airplane:
                case Storage.EntityModels.VehicleTypes.Ship:
                case Storage.EntityModels.VehicleTypes.Train:
                    rowAirplaneShipTrain1.Visible = true;
                    rowAirplaneShipTrain2.Visible = true;
                    rowCarMotorcycle1.Visible = false;
                    rowCarMotorcycle2.Visible = false;
                    rowCarMotorcycle3.Visible = false;
                    rowCarMotorcycle4.Visible = false;
                    rowOnlyCar.Visible = false;
                    break;
                case Storage.EntityModels.VehicleTypes.Bike:
                    rowAirplaneShipTrain1.Visible = false;
                    rowAirplaneShipTrain2.Visible = false;
                    rowCarMotorcycle1.Visible = false;
                    rowCarMotorcycle2.Visible = false;
                    rowCarMotorcycle3.Visible = false;
                    rowCarMotorcycle4.Visible = false;
                    rowOnlyCar.Visible = false;
                    break;
                case Storage.EntityModels.VehicleTypes.Motorcycle:
                case Storage.EntityModels.VehicleTypes.Car:
                    rowAirplaneShipTrain1.Visible = false;
                    rowAirplaneShipTrain2.Visible = false;
                    rowCarMotorcycle1.Visible = true;
                    rowCarMotorcycle2.Visible = true;
                    rowCarMotorcycle3.Visible = true;
                    rowCarMotorcycle4.Visible = true;
                    if (_val.Equals(Storage.EntityModels.VehicleTypes.Car))
                        rowOnlyCar.Visible = true;
                    else
                        rowOnlyCar.Visible = false;
                    break;
            }
            aggiorna.Update();
        }

    }
}